#include "N2Region.h"

N2Region::N2Region(const N2Rect &rect) : m_rect(rect), m_rects(NULL), 
                                         m_count(0)
{
}

N2Region::N2Region(const N2Region &r): m_rect(r.m_rect), m_count(r.m_count)
{
    m_rects = (N2Rect*)N1Malloc(m_count * sizeof(N2Rect));
    memcpy(m_rects, r.m_rects, m_count * sizeof(N2Rect));
}

N2Region::~N2Region()
{
    if(m_count)
    {
        N1Free(m_rects);
    }
}

N1NAME_DEF(N2Region)

N2Rect N2Region::getBounds() const 
{
    N2Rect ret;
    N2Rect *cur = m_rects;

    if(!m_rects) return m_rect;

    ret = *cur++;
    for(uint32 i = 1; i < m_count; i++)
    {
       ret.expand(*cur++);
    }

    return ret;
}

bool N2Region::isContains(const N2Point &point) const 
{
    return false;
}

bool N2Region::isContains(const N2Rect &rect) const 
{
    return false;
}

bool N2Region::isContains(const N2Region &rgn) const 
{
    return false;
}

N2RegionArea::N2RegionArea(const N2Region &rgn, const N2Rect &rect)
{
    m_nWidth = rect.width();
    m_nHeight = rect.height();
    m_nByteInRow = ( m_nWidth + 31) / 32 * 4;
    m_nSize = m_nByteInRow * m_nHeight;
    m_pData = new char[ m_nSize ];
    memset(m_pData, 0x00, m_nSize);

    N2Coord h, v;
    for(v=rect.top;v<=rect.bottom;v++)
    {
        for(h=rect.left;v<=rect.right;v++)
        {
            {
                if(rgn.isContains(N2Point(h, v)))
                {
                    setPoint(h, v);
                }
            }
        }
    }
}

void N2RegionArea::setPoint(N2Coord x, N2Coord y)
{
    BYTE *pRow = (BYTE*)m_pData + m_nByteInRow * y;
    *(pRow + x/8) |= 1 << x%8;
}

N2RegionArea::~N2RegionArea()
{
    delete []m_pData;
}

N1NAME_DEF(N2RegionArea)

